#include "delta.h"

int lambda(int **sb)
{

	int max_lambda, lambda;
	int a, b, x, F_x;

	int indices[64][2];
	for (int i = 0; i < 64; i++) {
		indices[i][0] = (i&1) | ((i & 32) >> 4);
		//printf("%i\t%i\t%i\n", indices[i][0], (i&32)>>4, i);
		indices[i][1] =
		    ((i & 2) >> 1) | ((i & 4) >> 1) | ((i & 8) >> 1) | ((i & 16)
									>> 1);
		//printf("%i\n", indices[i][1]);
	}

	int DIFF[64];
	for (int i = 0; i < 64; i++)
		DIFF[i] = norm(i, 6);

//for(int i = 0; i<64; i++)
//      printf("%i\n", DIFF[i]);

	max_lambda = 0;

	for (a = 0; a < 64; a++) {

		for (b = 1; b < 16; b++) {
			lambda = 0;
			for (x = 0; x < 64; x++) {
				F_x = sb[indices[x][0]][indices[x][1]];
				//if(a==63) printf("F_x : %i\n", F_x);
				if (DIFF[a & x] == DIFF[b & F_x]) {
					lambda += 1;
				}
			}
			lambda = (lambda >= 32) ? lambda - 32 : 32 - lambda;
			//printf("l(%i,%i) = %i\n", a, b, lambda);
			if (lambda > max_lambda)
				max_lambda = lambda;
		}
	}
	//printf("max_lambda : %i\n", max_lambda);
	return max_lambda;
}
